var initdata = function (that) {
  var list = that.data.address_list;
  for (var i = 0; i < list.length; i++) {
    list[i].txtStyle = ""
  }
  that.setData({ address_list: list })
}

let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    delBtnWidth: 180,
    address_list:[],
    num:0,
    is_show_time:1,
    nocancel: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    new app.ToastPannel();
    that.requests();
    that.initEleWidth();
  },
  onShow:function(){
  	var that = this
  	new app.ToastPannel();
  	that.requests();
  	that.initEleWidth();
  },
  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置  
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function (e) {
    var that = this;
    initdata(that)
    
    if (e.touches.length == 1) {
      
      //手指移动时水平方向位置  
      var moveX = e.touches[0].clientX;
      //手指起始点位置与移动期间的差值  
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth;
      var txtStyle = "";
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变  
        txtStyle = "left:0px";
      } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离  
        txtStyle = "left:-" + disX + "px";
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度  
          txtStyle = "left:-" + delBtnWidth + "px";
        }
      }
      //获取手指触摸的是哪一项  
      var index = e.currentTarget.dataset.index;
      var list = this.data.address_list;
      list[index].txtStyle = txtStyle;
      //更新列表的状态  
      this.setData({
        address_list: list
      });
    }
  },

  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置  
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离  
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮  
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
      //获取手指触摸的是哪一项  
      var index = e.currentTarget.dataset.index;
      var list = this.data.address_list;
      list[index].txtStyle = txtStyle;
     
      //更新列表的状态  
      this.setData({
        address_list: list
      });
    }
  },
  //获取元素自适应后的实际宽度  
  getEleWidth: function (w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应  
        
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error  
    }
  },
  initEleWidth: function () {
    var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
    this.setData({
      delBtnWidth: delBtnWidth
    });
   
  },
  //删除历史搜索记录
  delete_address: function (e) {
    var id = e.currentTarget.dataset.id
    console.log(id)
    this.setData({
      nocancel: true,
      address_id:id
    })
  },
  confirm: function () {
    var adrs_id = this.data.address_id
    this.del_address(adrs_id)
    this.setData({
      nocancel: false,
    })
  },
  cancel: function () {
    this.setData({
      nocancel: false,
    })
  },
  //删除
  del_address: function (adrs_id){
    var that = this
    var id = adrs_id
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')

          wx.request({
            url: app.configData.shop.api_url + '/index.php?ctl=Buyer_User&met=delAddress&typ=json',
            method: 'GET',
            data: { k: key, u: uid,id:id },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
             
              if (res.data.status == 200) {
                that.show('删除成功')
                that.requests()
                
              } else {
                that.show('删除失败')
              }

            
      }
    })

  },
  requests:function(){
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_User&met=address&typ=json',
      method: 'GET',
      data: { k: key, u: uid },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var num = 0
        for (var i in res.data.data.address_list){
          num++
        }
        if (res.data.status == 200) {
          that.setData({
            address_list: res.data.data.address_list,
            num:num,
            is_show_time:0
          })
        } else {
          that.show('数据加载失败')
         
        }

      }
    })
  },
  //地址选择
  address: function (e) {
    var address_id = e.currentTarget.dataset.address_id
    wx.navigateTo({
      url: '../buy_step1/buy_step1?address_id=' + address_id
    })
  },
  add_address:function(){
    wx.navigateTo({
      url: '../address_opera/address_opera',
    })
  },
  
  link_url:function(e){
    var user_address_id = e.currentTarget.dataset.user_address_id
    wx.navigateTo({
      url: '../address_opera_edit/address_opera_edit?user_address_id=' + user_address_id,
    })
  }

})